package com.itcam.lease.web.app.service.impl;

import com.itcam.lease.model.entity.*;
import com.itcam.lease.model.enums.ItemType;
import com.itcam.lease.web.app.mapper.*;
import com.itcam.lease.web.app.service.LeaseAgreementService;
import com.itcam.lease.web.app.vo.agreement.AgreementDetailVo;
import com.itcam.lease.web.app.vo.agreement.AgreementItemVo;
import com.itcam.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.messages.Item;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    /**
     * 获取个人租约基本信息列表
     * @param phone
     * @return
     */
    @Override
    public List<AgreementItemVo> listItemByPhone(String phone) {
        return leaseAgreementMapper.listItemByPhone(phone);
    }

    /**
     * 根据id获取租约详细信息
     * @param id 租约id
     * @return 租约详细信息
     */
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        // 1.查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        // 如果租约信息不存在，则返回null
        if (leaseAgreement == null) {
            return null;
        }
        // 2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());
        // 3.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        // 4.查询图片信息
        // 查询房间图片信息
        List<GraphVo> roomGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, leaseAgreement.getRoomId());
        // 查询公寓图片信息
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        // 5.查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
        // 6.查询租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
        // 7.封装返回结果
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        // 7.1 将租约信息复制到租约详细信息对象中
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        // 7.2 设置公寓名称
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        // 7.3 设置房间编号
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        // 7.4 设置房间图片信息
        agreementDetailVo.setApartmentGraphVoList(roomGraphVoList);
        // 7.5 设置公寓图片信息
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        // 7.6 设置支付方式名称
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        // 7.7 设置租期月数
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        // 7.8 设置租期单位
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }

    /**
     * 根据房间id获取可选支付方式列表
     * @param id
     * @return
     */
    @Override
    public List<PaymentType> listByRoomId(Long id) {
        return paymentTypeMapper.selectListByRoomId(id);
    }

}




